<?php 
/**
	Author: Gustavo Palacios
	Author: Fernando Rodas

	Project: EPS, Ciencias y Sistemas, USAC. 2014-2015	

	CLASE QUE CONTIENE TODAS LAS FUNCIONES NECESARIAS PARA LA CONSTRUCCION 
	DE LAS DISTINTAS SECCIONES DE LAS PAGINAS

*/
include_once("dbMng.php");
	class Funciones_bd extends DataBaseManager{

	/**
		Constructor
	*/
		function __construct(){
		//$serverName, $port, $db_name, $username, $password
		$serverName = "localhost";
		$port = "3306";
		$db_name = "cema_administrative";
		$username = "cema";
		$password = "Administrativo2014$";
		parent::__construct($serverName, $port, $db_name, $username, $password);
	}

	
		//Funcion para ejecutar cualquier query
		function ExecutePersonalizado($sql){		
			$result = $this->link->mysqli_query($sql);			
			if( $result === false) {
				die(print_r(mysqli_error($this->link), true));
			}
			return $result;
		}

		//--> SELECT (Consultar)
		function Consultar($strTablas, $strCampos, $strRestricciones, $strAgrupacion, $strOrdenamiento){
			$strSql = "";
			$result;
				//Se arma la consultar
			$strSql = " SELECT " . $strCampos . " FROM " . $strTablas . " ";

			($strRestricciones != "") ? $strSql = $strSql. " WHERE " . $strRestricciones . " " : $strSql = $strSql;
			($strAgrupacion != "") ? $strSql = $strSql. " GROUP BY " . $strAgrupacion . " " : $strSql = $strSql;
			($strOrdenamiento != "") ? $strSql = $strSql. " ORDER BY " . $strOrdenamiento . " " : $strSql = $strSql;

			//se ejecuta la consulta
			try {
				$result = mysql_query($strSql) or die(mysql_error());
				return $result;				
			}catch(Exception $e) {
				echo 'Error: ' .$e->getMessage();
			}			
		}

		//--> INSERT (Insertar)
		function Insertar($strTabla, $strCampos, $strValores){
			$strSql = "";			
			$strSql = " INSERT INTO " . $strTabla . "(" . $strCampos . ") VALUES(" . $strValores . ")";
			//se ejecuta la consulta
			try {
				$result = mysql_query($strSql);
				if($result){
					return 1;
				}else{
					return $result;
				}				
			}catch(Exception $e) {
				return $e;
			}
		}

		//--> DELETE (Eliminar)
		function Eliminar($strTabla, $strRestricciones){
			$strSql = "";
			$strSql = " DELETE FROM " . $strTabla . " ";
			if($strRestricciones != ""){
				$strSql = $strSql . " WHERE " . $strRestricciones . " ";
			}
			try {
				$result = mysql_query($strSql);
				if($result){
					return 1;
				}else{
					return -1;
				}				
			}catch(Exception $e) {
				return -1;
			}
		}

		//--> UPDATE (Modificar)
		function Modificar($strTabla, $strValores ,$strRestricciones){
			$strSql = "";
			$strSql = " UPDATE " . $strTabla . " SET " . $strValores . " ";
			if ($strRestricciones != "") {
				$strSql = $strSql. " WHERE " . $strRestricciones . " ";
		
			}
			try {
				$result = mysql_query($strSql);
				if($result){
					return 1;
				
				}else{
					return -1;
				}
			}catch(Exception $e) {
				return -1;
			}
		}
	}
	?>